<?php
class Header_Loginform extends TdPage {
	
	
	public $formlogin;		
	public $formrecover;	
	
	
	function __construct($template) {
		parent::__construct("", $template);
		$this->doInit();
	}
	
	
	function doInit() {	
		global $js;			
		
		
		$js->addJS("../public/js/handle_passw.js");
		
			
		if (isset($_SESSION["user"])) {
			$this->createWelcome();
		} else {
			$this->createform();
		}	

		//session_start();
	}
	
	
	
	function createWelcome() {
		
		global $webroot;
		
		if (isset($_SESSION["user"])) {
			
			$utility = new cl_Utility();
			$this->welcome = $utility->doGetLogin();
			
			//$formlogin = "<p class=\"login_text\"><a href=\"".$this->classname."\$Header_Loginform+logout.htm\">Logout</a></p>";
			$formlogin = "<p class=\"login_text\"><a href=\"$webroot/login+logout.htm\">Logout</a></p>";
				
			$formlogin .= "<p class=\"ext login_text\">"._("Welcome").",&nbsp;".$this->welcome."!</p>";
		
		} else {
			
			$formlogin = _("An error occurred");
		}
		
		$this->formlogin = $formlogin;
		
	}
	
	function createform() {
	
		global $webroot;
	
		$formlogin = new TdForm("form_login", "../core/app/form/form.phtml");
		$formlogin->setName("form_login");
		$formlogin->setMethod("POST");
		$formlogin->setAction($webroot."/login.htm");		
		
		$loginhtmlcode1 = new TdHtmlCode("loginhtmlcode1", "../core/app/form/htmlcode.phtml");
		$code1 = "<p class=\"login_text\"><a href=\"".tdUrl("login.htm")."\">"._("Log in")."</a>&nbsp;"._("to manage your profile, or")."&nbsp;<a href=\"".tdUrl("signup.htm")."\">"._("sign up")."</a>&nbsp;"._("to start with us")."</p>";
		$loginhtmlcode1->setValue($code1);
		$formlogin->addField($loginhtmlcode1);
		
		
		            
	
	
		$hidden_login = new TdHiddenField("hidden_login", "../core/app/form/hiddenfield.phtml");
		$hidden_login->setValue("form_login");
		$hidden_login->setName("hidden_form");
		$formlogin->addField($hidden_login);
	
	
	
		$username = new TdTextField("username", "../core/app/form/textfield.phtml");
		//$username->setRequired(true);
		$username->setValue("Username");
		$username->setName("username");
		$username->setSize("13");
		$username->setClass("input_login");
		$formlogin->addField($username);
			
		$password = new TdPasswordField("password", "../core/app/form/passwfield.phtml");
		//$password->setRequired(true);
		$password->setValue("");
		$password->setName("password");
		$password->setSize("13");
		$password->setClass("input_login");
		$password->setJs('style="display: none"; onblur="pwdBlur()"');
		$formlogin->addField($password);
	
		$fakepassword = new TdTextField("fakepassword", "../core/app/form/textfield.phtml");	
		//$fakepassword->setRequired(true);
		$fakepassword->setValue("Password");
		$fakepassword->setName("fakepassword");
		$fakepassword->setSize("13");
		$fakepassword->setClass("input_login");
		$fakepassword->setJs('onfocus="pwdFocus()"');
		$formlogin->addField($fakepassword);
	
	
		$l_check_login = new TdLabel("label_check_login", "../core/app/form/label.phtml");
		$l_check_login->setClass("label_check_login");
		$l_check_login->setValue(_("Remember Me"));
	
	
		$submit = new TdSubmitField("submit_login", "../core/app/form/submit.phtml");
		$submit->setValue("");
		$submit->setName("login");
		$submit->setClass("button_login");
		$formlogin->addField($submit);
	
	
		$loginhtmlcode = new TdHtmlCode("loginhtmlcode", "../core/app/form/htmlcode.phtml");
		//$code = "<div class=\"login_text2\"><a href=\"#\" onclick=\"show_recover()\">"."Forgot your password?"."</a></div>";
		$code = "<div class=\"login_text2\"><a href=\"".tdUrl("recover.htm")."\" >"._("Forgot your password?")."</a></div>";
		$loginhtmlcode->setValue($code);
		$formlogin->addField($loginhtmlcode);
	
	
	
	
		$check_login = new TdCheckboxField("check_login", "../core/app/form/checkboxfield.phtml");
		$check_login->setLabel($l_check_login);
		$check_login->setDivClass("label_checkbox_login");
		$check_login->setChecked(TRUE);
		$formlogin->addField($check_login);
			
			
		$this->addComponent($formlogin);
		$this->formlogin = $formlogin;
	
	}
	
	
	
	
	
	function doValidate() {
		global $teardrop;  	
	  	
	  	parent::doValidate();	  		  	
	  	
	  	$utility = new cl_Utility();
	
	  	if (!$this->formlogin->gotError()) {
	  		
	  		$utility = new cl_Utility();
	  		
	  		$res = $utility->doCheckLogin($this->formlogin->getField("username")->getValue(), $this->formlogin->getField("password")->getValue());
	  		
	  		if ( $res ) {	
	  			  		  	
	  			$user = new cl_User("id", $res);	  			
	  			
	  			// provvisorio
	  			if (!isset($_SESSION["user"])) {
	  				 $_SESSION["user"] = $user;
	  			}
	  			
	  			$teardrop->route("welcome.htm");
	  		
	  		} 
	      
	  	} 	  	
	
	}

}
?>